package com.cn.shchuangge.ui.activity;

import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.cn.shchuangge.projectwork.R;
import com.cn.shchuangge.ui.view.MyVideoView;

/**
 * Created by 赵航 on 2017/6/15.
 */

public class SplashActivity extends AppCompatActivity implements View.OnClickListener {

    private MyVideoView mVideoView;
    private Button mButtonExperience;
    private TextView mText;
    private   int time=5;
    private ImageView mImageView;
    private Handler mHandler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_video);
        //初始化view
        initView();

    }

    private void initView() {
        mVideoView = (MyVideoView) findViewById(R.id.splash_vidoview);
        mButtonExperience = (Button) findViewById(R.id.btn_experience);
        mText = (TextView) findViewById(R.id.shash_text);
        mImageView = (ImageView) findViewById(R.id.splash_imageview);
        mVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video));
        //播放
        mVideoView.start();
        //循环播放
        mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {

                mVideoView.start();
            }
        });



        //自定义5秒倒计时

        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {

                if (time<=0){
                    //TODO:跳转到主界面
                    //并且关闭倒计时
                    mHandler.removeCallbacksAndMessages(null);
                    intentToMainactivity();
                    return;
                }

                mText.setText(time+"秒");
                time--;
                mHandler.postDelayed(this, 1000);

            }
        }, 1000);

        //点击的时候会有进入登录界面或者商城的主界面
        mButtonExperience.setOnClickListener(SplashActivity.this);
        mImageView.setOnClickListener(SplashActivity.this);
        mText.setOnClickListener(SplashActivity.this);



    }

    //跳转到主界面并且结束当前页
    private void intentToMainactivity() {
        if (mHandler!=null) {
            mHandler.removeCallbacksAndMessages(null);
        }
        startActivity(new Intent(SplashActivity.this,MainActivity.class));

        finish();
    }

    //返回重启加载
    @Override
    protected void onRestart() {
        //必须先判空
        if (mVideoView!=null&&mVideoView.isPlaying()) {
            mVideoView.stopPlayback();
        }
        initView();

        super.onRestart();
    }

    //防止锁屏或者切出的时候，音乐在播放
    @Override
    protected void onStop() {


        //必须先判空
        if (mVideoView!=null&&mVideoView.isPlaying()) {
            mVideoView.stopPlayback();
        }
        //防止内存泄漏
        if (mHandler!=null) {
            mHandler.removeCallbacksAndMessages(null);
        }
        super.onStop();


    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.splash_imageview:
            case R.id.shash_text:
            case R.id.btn_experience:
                intentToMainactivity();
                break;

        }
    }

}
